package juc.executors;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class ThreadPoolExecutorLongTimeTask {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(10, 20, 30, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10), r -> {
            Thread thread = new Thread(r);
            //设置成守护线程，main线程结束则该线程结束
            thread.setDaemon(true);
            return thread;
        }, new ThreadPoolExecutor.AbortPolicy());
        //pall，并行处理任务
        IntStream.range(0, 10).boxed().forEach(i -> {
            //execute()非阻塞方法
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "->" + i + " finish done.");
                while (true) {
                }
            });
        });
        //seq，串行处理任务
        //executorService.shutdown();
        executorService.shutdownNow();
        executorService.awaitTermination(5,TimeUnit.SECONDS);
        System.out.println("start the sequence working");
    }
}
